package com.edge.pbx;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.edge.pbxdsp.R;

import com.hanlink.dsp.DspContext;

import com.hanlink.dsp.ui.CaAmpDspActivity;
import com.qztech.utils.YzDebug;

import java.lang.ref.WeakReference;



public class PbxHomeActivity extends AppCompatActivity {

    private static HomeHandler mHandler;
    private int mClickCount = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pbx_home);
        mHandler = new HomeHandler(this);
        findViewById(R.id.pbx_home_content_layout).setOnClickListener(v->{
            goMain();
        });
      //  mHandler.sendEmptyMessageDelayed(HomeHandler.WHAT_START_MAIN_ACTIVITY, BuildConfig.DEBUG?20:1500);

        mHandler.sendEmptyMessageDelayed(HomeHandler.WHAT_START_MAIN_ACTIVITY, 1500);
        TextView tv = findViewById(R.id.pbx_home_version_image);
        PackageInfo activityInfo = null;
        try {
            activityInfo = getPackageManager().getPackageInfo(getPackageName(),0);
            tv.setText("version "+activityInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }

        tv.setOnClickListener(v->{
            mClickCount++;
            mHandler.removeMessages(HomeHandler.WHAT_RESET_CLICK_COUNT);
            if (mClickCount >= 6){
                SPUtils.getInstance(Context.MODE_PRIVATE).put(DspContext.KEY_PWD, DspContext.PASSOWRD);
                setPasswordResetDialog(this);
                YzDebug.d("onCreate  mClickCount:"+ mClickCount);
                mClickCount = 0;
            }else {
                mHandler.sendEmptyMessageDelayed(HomeHandler.WHAT_RESET_CLICK_COUNT,1000);
            }

        });
    }
    private void goMain() {
        mHandler.removeCallbacksAndMessages(null);
        Intent intent = new Intent(this, CaAmpDspActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mHandler.removeCallbacksAndMessages(null);
    }

    public void onClickLink1(View view) {
        openLink("https://www.facebook.com/powerbassusainc/");
    }
    public void onClickLink2(View view) {
        openLink("https://twitter.com/powerbassusa");
    }
    public void onClickLink3(View view) {
        openLink("https://www.instagram.com/powerbassusa/?hl=en");
    }
    public void onClickLink4(View view) {
        openLink("https://www.youtube.com/user/PowerbassCarAudio?app=desktop");
    }
    public void onClickLink5(View view) {
        openLink("http://www.powerbassusa.com");
    }
    private void openLink(String link){
        Intent intent = new Intent();
        intent.setData(Uri.parse(link));
        intent.setAction(Intent.ACTION_VIEW);
        startActivity(intent);
    }
    private final static class HomeHandler extends Handler{
        private final static int WHAT_RESET_CLICK_COUNT = 0x100;
        private final static int WHAT_START_MAIN_ACTIVITY = 0x101;
        private WeakReference<PbxHomeActivity> mHomeActivityWeakReference;
        public HomeHandler(PbxHomeActivity activity){
            super(activity.getMainLooper());
            mHomeActivityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            PbxHomeActivity activity = mHomeActivityWeakReference.get();
            switch (msg.what){
                case WHAT_RESET_CLICK_COUNT:
                    if (activity != null){
                        activity.mClickCount = 0;
                    }
                    break;
                case WHAT_START_MAIN_ACTIVITY:
                    if (activity != null){
                        activity.goMain();
                    }
                    break;
            }
        }
    }
    private void setPasswordResetDialog(Context ctx) {
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(ctx, com.qztech.sdk.R.style.dialog);
        final View view = LayoutInflater.from(ctx).inflate(R.layout.dialog_password_reset_success, null, false);
        androidx.appcompat.app.AlertDialog alertDialog = builder.setView(view)
//                .setTitle(R.string.title_change_password)
                .setCancelable(false)
                .create();
        view.findViewById(R.id.ok).setOnClickListener(v -> {
            alertDialog.dismiss();
        });
        alertDialog.show();

    }
}